package com.samebuilding.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.samebuilding.domain.Activity;
import com.samebuilding.domain.Chat;

/**
 * A data access object (DAO) providing persistence and search support for Chat
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see com.samebuilding.domain.Chat
 * @author MyEclipse Persistence Tools
 */

public class ChatDAOImpl extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(ChatDAOImpl.class);
	// property constants
	public static final String CHAT_NAME = "chatName";
	public static final String CHAT_MESSAGE = "chatMessage";
	public static final String CREATOR_ID = "creatorId";
	public static final String CREATOR_NAME = "creatorName";
	public static final String MANAGERS_ID = "managersId";
	public static final String MEMBERS_ID = "membersId";
	public static final String MEMBER_COUNT = "memberCount";
	public static final String TYPE = "type";

	protected void initDao() {
		// do nothing
	}

	public void save(Chat transientInstance) {
		log.debug("saving Chat instance");
		try {
			getHibernateTemplate().save(transientInstance);
			
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Chat persistentInstance) {
		log.debug("deleting Chat instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Chat findById(java.lang.Integer id) {
		log.debug("getting Chat instance with id: " + id);
		try {
			Chat instance = (Chat) getHibernateTemplate().get(
					"com.samebuilding.domain.Chat", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Chat instance) {
		log.debug("finding Chat instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Chat instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Chat as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByChatName(Object chatName) {
		return findByProperty(CHAT_NAME, chatName);
	}

	public List findByChatMessage(Object chatMessage) {
		return findByProperty(CHAT_MESSAGE, chatMessage);
	}

	public List findByCreatorId(Object creatorId) {
		return findByProperty(CREATOR_ID, creatorId);
	}

	public List findByCreatorName(Object creatorName) {
		return findByProperty(CREATOR_NAME, creatorName);
	}

	public List findByManagersId(Object managersId) {
		return findByProperty(MANAGERS_ID, managersId);
	}

	public List findByMembersId(Object membersId) {
		return findByProperty(MEMBERS_ID, membersId);
	}

	public List findByMemberCount(Object memberCount) {
		return findByProperty(MEMBER_COUNT, memberCount);
	}

	public List findByType(Object type) {
		return findByProperty(TYPE, type);
	}

	public List findAll() {
		log.debug("finding all Chat instances");
		try {
			String queryString = "from Chat";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public List findAllChat(final int startRecord,final int maxRecord,final String chatName,final String name) {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String hql="from Chat as p";
				
				if(!chatName.equals(""))
				 {
					 hql=hql+" where p.chatName like '%"+chatName+"%'";
					 if(!name.equals(""))
					 {
						 hql=hql+" and p.creatorName like '%"+name+"%'";
					 }
				 }
				 else if(!name.equals(""))
				 {
					 hql=hql+" where p.creatorName like '%"+name+"%'";
				 }
				 hql=hql+" order by p.type ";
				Query query = session.createQuery(hql);
				query.setFirstResult(startRecord);
				query.setMaxResults(maxRecord);
				List<Activity> list = query.list();
				return list;
			}
		});
		return list;
	}
	public List findAllChatDescId() {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String hql="from Chat as p ";
				
				
				 hql=hql+"where p.state=1 order by p.chatId desc";
				Query query = session.createQuery(hql);
				List<Activity> list = query.list();
				return list;
			}
		});
		return list;
	}
	public List findAllChatDescmeMberCount() {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				String hql="from Chat as p ";
				
				
				 hql=hql+"where p.state=1 order by p.memberCount desc";
				Query query = session.createQuery(hql);
				List<Activity> list = query.list();
				return list;
			}
		});
		return list;
	}
	public List findCount(String chatName,String name) {
		log.debug("finding all Chat instances");
		try {
			String hql = "select count(*) from Chat as p";
			if(!chatName.equals(""))
			 {
				 hql=hql+" where p.chatName like '%"+chatName+"%'";
				 if(!name.equals(""))
				 {
					 hql=hql+" and p.creatorName like '%"+name+"%'";
				 }
			 }
			 else if(!name.equals(""))
			 {
				 hql=hql+" where p.creatorName like '%"+name+"%'";
			 }
			return getHibernateTemplate().find(hql);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public Chat merge(Chat detachedInstance) {
		log.debug("merging Chat instance");
		try {
			Chat result = (Chat) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Chat instance) {
		log.debug("attaching dirty Chat instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Chat instance) {
		log.debug("attaching clean Chat instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ChatDAOImpl getFromApplicationContext(ApplicationContext ctx) {
		return (ChatDAOImpl) ctx.getBean("ChatDAO");
	}
}